package top.ljjapp.security.browser;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;
import top.ljjapp.security.core.authentication.AbstractChannelSecurityConfig;
import top.ljjapp.security.core.authentication.mobile.SmsCodeAuthenticationSecurityConfig;
import top.ljjapp.security.core.properties.SecurityConstants;
import top.ljjapp.security.core.properties.SecurityProperties;
import top.ljjapp.security.core.validate.code.ValidateCodeSecurityConfig;

import javax.sql.DataSource;

@Configuration
public class BrowserSecurityConfig extends AbstractChannelSecurityConfig {

    /**
     * 自定义安全配置
     */
    @Autowired
    private SecurityProperties securityProperties;

    /**
     * 短信验证码的security配置
     */
    @Autowired
    private SmsCodeAuthenticationSecurityConfig smsCodeAuthenticationSecurityConfig;

    /**
     * 验证码的security配置
     */
    @Autowired
    private ValidateCodeSecurityConfig validateCodeSecurityConfig;
    /**
     * 添加数据源
     */
    @Autowired
    private DataSource dataSource;

    @Autowired
    private UserDetailsService userDetailsService;

    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    /**
     * tokenRepository
     * @return
     */
    @Bean
    public PersistentTokenRepository persistentTokenRepository() {
        JdbcTokenRepositoryImpl tokenRepository = new JdbcTokenRepositoryImpl();
        tokenRepository.setDataSource(dataSource);
        //启动的时候自动建表
//        tokenRepository.setCreateTableOnStartup(true);
        return tokenRepository;
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
//        http.httpBasic() //默认弹出的框的登录
        //表单登录的配置
        applyPasswordAuthenticationConfig(http);
        http.apply(validateCodeSecurityConfig) //验证码过滤器
//            .and()
//                .apply(smsCodeAuthenticationSecurityConfig) //短信验证码过滤器
            .and()
                .authorizeRequests() //授权管理
                .antMatchers(SecurityConstants.DEFAULT_UNAUTHENTICATION_URL,
                        SecurityConstants.DEFAULT_LOGIN_PROCESSING_URL_MOBILE,
                        securityProperties.getBrowser().getLoginPage(),
                        SecurityConstants.DEFAULT_VALIDATE_CODE_URL_PREFIX + "/*",
                        "http://localhost:8080/static/*").permitAll() //匹配自定义页面的授权,此处允许所有角色访问
                .anyRequest()   //除上面那些链接所有请求
                .authenticated() //允许认证过的用户访问
            .and()
                .rememberMe() //记住我
                .tokenRepository(persistentTokenRepository())  //配置数据源
                .tokenValiditySeconds(securityProperties.getBrowser().getRememberMeSeconds()) //过期时间
                .userDetailsService(userDetailsService)
            .and()
                .csrf().disable()//跨站伪造防护
        ;


    }
}
